package com.linjuke.childay.util;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class IoUtil {

	private static final int BUF_SIZE = 8 * 1024;
	
	public static String getContent(Reader reader) throws IOException {
		StringWriter writer = new StringWriter();
		ioAndClose(reader, writer);
		return writer.toString();
	}
	
	public static String getContent(InputStream is, String charset) throws IOException {
		return getContent(new InputStreamReader(is, charset));
	}
	
	public static void io(Reader reader, Writer writer)  throws IOException {
		char[] buf = new char[BUF_SIZE];
		while (true) {
			int len = reader.read(buf);
			if (len < 0) {
				break;
			}
			writer.write(buf, 0, len);
		}
	}
	
	public static void ioAndClose(Reader reader, Writer writer) throws IOException {
		io(reader, writer);
		reader.close();
		writer.close();
	}
	
	public static void io(InputStream is, OutputStream os) throws IOException {
		byte[] buf = new byte[BUF_SIZE];
		while (true) {
			int len = is.read(buf);
			if (len < 0) {
				break;
			}
			os.write(buf, 0, len);
		}
	}
	
	public static byte[] readAll(InputStream is) throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		io(is, bos);
		return bos.toByteArray();
	}
	
	public static void ioAndClose(InputStream is, OutputStream os) throws IOException {
		io(is, os);
		is.close();
		os.close();
	}
	
	public static byte[] readFromUrl(URL url) throws IOException {
		if (url == null) {
			return null;
		}
		try {
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			InputStream is = conn.getInputStream();
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ioAndClose(is, bos);
			return bos.toByteArray();
		} catch(FileNotFoundException e) {
			Log.e("io error", e.getMessage(), e);
			return null;
		}
	}
	
	public static void closeQuietly(Closeable c) {
		if (c != null) {
			try {
				c.close();
			} catch (IOException e) {
				// ignore
			}
		}
	}
	
}
